<?php


namespace app\common\model;

use think\Db;
use think\Model;

class Money extends Model
{

    //添加余额
    public static function addMoney($user_id,$money)
    {
        $res=Db::table('user')->where([
            'id'=>$user_id
        ])->setInc('money',$money);

        return $res;
    }

    //添加余额
    public static function addCanTakeOutMoney($user_id,$money)
    {
        $res=Db::table('user')->where([
            'id'=>$user_id
        ])->setInc('can_take_out',$money);

        return $res;
    }


    //提现操作
    public static function takeOut($user_id,$money,$alipay_name=0,$alipay_account=0)
    {
        //数据的校验需要在model之外处理

        //开始提现的操作
        Db::startTrans();
        try {
            //在可提现金额
            $res=Db::table('user')->where([
                'id'=>$user_id
            ])->setDec('can_take_out',$money);

            //流水存记录
            $res2=Db::table('money_take_out')->insert([
                'user_id'=>$user_id,
                'money'=>$money,
                'alipay_name'=>$alipay_name,
                'alipay_account'=>$alipay_account,
                'add_time'=>time(),
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            exception($e->getMessage());
             return false;    //失败了，返回false
        }
    }

    //计算可提现金额
    public static function calcCanTakeOutMoney()
    {

    }

}
